package zyy.wcx.util;

/**
 * @program: data-structures-algorithms
 * @description: 数据结构工具类
 * @author: WuChengXing
 * @create: 2020-07-07 00:23
 **/
public class DataStructureUtils {

    /**
     * 功能描述: 将正常的二维数组转换成稀疏数组
     *
     * @param normalArray
     * @return int[][]
     * @author 吴呈兴
     * @date 2020/7/7
     */
    public static int[][] NormalConvertToSparse(int[][] normalArray) {

        // 统计出有多少不为0的数据
        int sum = 0;
        for (int i = 0; i < normalArray.length; i++) {
            for (int j = 0; j < normalArray.length; j++) {
                if (normalArray[i][j] != 0) {
                    sum++;
                }
            }
        }
        // 初始化稀疏数组
        int[][] sparseArr = new int[sum + 1][3];
        sparseArr[0][0] = 11;
        sparseArr[0][1] = 11;
        sparseArr[0][2] = sum;

        int count = 0;
        for (int i = 0; i < normalArray.length; i++) {
            for (int j = 0; j < normalArray.length; j++) {
                if (normalArray[i][j] != 0) {
                    count++;
                    sparseArr[count][0] = i;
                    sparseArr[count][1] = j;
                    sparseArr[count][2] = normalArray[i][j];
                }
            }
        }
        return sparseArr;
    }

    /**
     * 功能描述: 将稀疏数组转换成正常的二维数组
     *
     * @param sparseArray
     * @return int[][]
     * @author 吴呈兴
     * @date 2020/7/7
     */
    public static int[][] SparseConvertToNormal(int[][] sparseArray) {
        int[][] normalArray = new int[sparseArray[0][0]][sparseArray[0][1]];
        for (int i = 1; i < sparseArray.length; i++) {
            normalArray[sparseArray[i][0]][sparseArray[i][1]] = sparseArray[i][2];
        }
        return normalArray;
    }
}
